﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Meeting.DBUtility;
using Meeting.Model;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace Meeting.BLL
{
    public class SubOffice
    {
        public string errorString = "";

        //获取会议室表数据
        private DataSet GetSubOffice(string filter)
        {
            string sql = "Select  * SubOffice Where 1=1 ";
            if (!string.IsNullOrEmpty(filter)) sql += " And " + filter;
            try
            {
                return DbHelperSQL.Query(sql);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return null;
            }
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public string GetPage(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;

            DataTable dt = DbHelperSQL.GetPage(" Id  ", "  *  ", " dbo.SubOffice   ", filter, " Number ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            JsonGrid jr = new JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;
            return JsonConvert.SerializeObject(jr);
        }
        /// <summary>
        /// 编号不重复
        /// </summary>
        public bool Exists(int number)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SubOffice");
            strSql.Append(" where Number=@Number ");
            SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.Int,4)};
            parameters[0].Value = number;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Meeting.Model.SubOffice model)
        {
            //if (Exists(model.Number))
            //{
            //    errorString = "已存在该编号";
            //    return false;
            //}

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SubOffice(");
            strSql.Append("Number,Name)");
            strSql.Append(" values (");
            strSql.Append("@Number,@Name)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,128)};
            parameters[0].Value = model.Number;
            parameters[1].Value = model.Name;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private int GetOldNumber(int id)
        {
            string sql = "Select Number From SubOffice Where Id = '" + id + "' ";
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds == null || ds.Tables[0].Rows.Count < 1) return -1;
            try
            {
               return  Convert.ToInt32( ds.Tables[0].Rows[0]["Number"].ToString());
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Meeting.Model.SubOffice model)
        {
            //int oldNumber = GetOldNumber(model.Id);
            //if (oldNumber == -1)
            //{
            //    errorString = "获取原始编号出错";
            //    return false;
            //}
            ////
            //if (oldNumber != model.Number)
            //{
            //    if (Exists(model.Number))
            //    {
            //        errorString = "已存在该编号，请修改编号";
            //        return false;
            //    }
            //}
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SubOffice set ");
            strSql.Append("Number=@Number,");
            strSql.Append("Name=@Name");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.NVarChar,128),
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Number;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SubOffice ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SubOffice ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        ///获取选择树 用OA user 类
        public string GetSubOfficeJson(string sf)
        {
           
            string sql = "Select * From SubOffice Where 1=1  ";
            if (sf != "") sql += " And " +sf;
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds == null || ds.Tables[0].Rows.Count < 1) return "";

            List<OaUser> list = new List<OaUser>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                //添加父节点
                OaUser tUser = new OaUser();
                tUser.parent = "";
                tUser.account = dr["Id"].ToString();
                tUser.name = dr["Name"].ToString();  
                tUser.nodetype = "";
                tUser.title = dr["Number"].ToString(); ; ;
                list.Add(tUser);
               
            }
            return  JsonConvert.SerializeObject(list);
           
        }

        public string GetSubOfficeJosnForGrid()
        {
            string sql = @"Select *,'' As Value ,'100' As Sort From SubOffice ";
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds == null) return "";
            //
            JsonGrid jr = new JsonGrid();
            jr.total = ds.Tables[0].Rows.Count;
            jr.rows = ds.Tables[0]; 
            return JsonConvert.SerializeObject(jr ); 
        }
    }
}
